/**
 * Created by chenzui on 2017/4/7.
 */
var $offerName = $('#offer_name');
var $type = $('#type');
var $brand = $('#offer_brand');
var $inPrice = $('#in_price');
var $outPrice = $('#out_price');
var $saleCnt = $('#saleCnt');
var $colorBoxes = $("input[name=color]");
var $lengthBoxes = $("input[name=length]");
var imgUrls = "";
function validate() {
    if($offerName.val().trim() == ""){
        alert("请输入商品名称");
        return false;
    }
    if($type.val().trim() == ""){
        alert("请输入类别");
        return false;
    }
    // if($brand.val().trim() == ""){
    //     alert("选择品牌");
    //     return false;
    // }
    // if($market.val().trim() == ""){
    //     alert("选择专场");
    //     return false;
    // }
    if($inPrice.val().trim() == ""){
        alert("请输入进货价格");
        return false;
    }
    if($outPrice.val().trim() == ""){
        alert("请输入售价");
        return false;
    }
    if($saleCnt.val().trim() == ""){
        alert("请输入起售数量");
        return false;
    }
    if($colorBoxes.size() == 0 && $lengthBoxes.size() == 0){
        alert("请选择商品属性");
        return false;
    }
    return true;

}
function addOffer() {
    debugger;
    if(validate()){
        var colorArr = "";
        $.each($colorBoxes,function (i,v) {
            if(v.checked == true){
                colorArr += v.value+","
            }
        })
        colorArr = colorArr.substr(0,colorArr.length-1);

        var lengthArr = "";
        $.each($lengthBoxes,function (i,v) {
            if(v.checked == true){
                lengthArr += v.value+","
            }
        })
        lengthArr = lengthArr.substr(0,lengthArr.length-1);
        var param = {
            offerName:$offerName.val().trim(),
            type:$type.val(),
            brand:$brand.val(),
            inPrice:$inPrice.val().trim(),
            outPrice:$outPrice.val().trim(),
            saleCnt:$saleCnt.val().trim(),
            colorArr:colorArr,
            lengthArr:lengthArr,
            imgUrls:imgUrls.substr(0,imgUrls.length-1),
            content:editor.$txt.html()
        }
        Ajax.ajax({
            url: "/offer/add",
            data: param,
            type: "post",
            dataType: 'json',
            success:function(res){
                console.log(res);
                if(res.resultCode == 0){
                    alert("商品新增成功");
                }
            }

        });
    }
}
function updateOffer(offerId) {
    if(validate()){
        var colorArr = "";
        $.each($colorBoxes,function (i,v) {
            if(v.checked == true){
                colorArr += v.value+","
            }
        })
        colorArr = colorArr.substr(0,colorArr.length-1);

        var lengthArr = "";
        $.each($lengthBoxes,function (i,v) {
            if(v.checked == true){
                lengthArr += v.value+","
            }
        })
        lengthArr = lengthArr.substr(0,lengthArr.length-1);
        var param = {
            id:offerId,
            offerName:$offerName.val().trim(),
            type:$type.val(),
            brand:$brand.val(),
            inPrice:$inPrice.val().trim(),
            outPrice:$outPrice.val().trim(),
            saleCnt:$saleCnt.val().trim(),
            colorArr:colorArr,
            lengthArr:lengthArr,
            imgUrls:imgUrls.substr(0,imgUrls.length-1),
            content:editor.$txt.html()
        }
        Ajax.ajax({
            url: "/offer/updateOffer",
            data: param,
            type: "post",
            dataType: 'json',
            success:function(res){
                console.log(res);
                if(res.resultCode == 0){
                    alert("商品信息更新成功");
                }
            }

        });
    }

}

$(function () {
    $('img').hover(function(){
        $(this).find("i").show();
    },function(){
        $(this).find("i").hide();
    });

    $('.form-group .col-sm-6 li i').on('click',function () {
        var $i = $(this);
        console.log($i.attr("data-img-id"));
        if(confirm("确认删除该图片吗")){
            Ajax.ajax({
                url: "/offer/deleImg",
                data: {imgId:$(this).attr("data-img-id")},
                type: "post",
                dataType: 'json',
                success:function(res){
                    console.log(res);
                    if(res.resultCode == 0){
                        $i.parent().remove();
                    }
                }

            });
        }
    });
    $('#submitBtn').on('click',function () {
        var btn = $(this);
        var offerId = btn.attr("offerId");
        if(offerId == null || offerId == undefined || offerId == '$initData.offer.id'){
            addOffer();
        }else {
            updateOffer(offerId);
        }
    });

    $('#offer-detail-cancel-btn').on('click',function () {
        $('#mainframe').load('/shop/offer/offer/list.html');
    });
    var imgdata=[];
    $("#file_sequence_id_1").change(function(){
        var file = this.files[0];
        if(!/image\/\w+/.test(file.type)){
            alert("文件必须为图片！");
            return false;
        }

        var tmp = $(this);
        var id = tmp.attr("id");
        $.ajaxFileUpload({
            url:'/file/upload',            //需要链接到服务器地址
            fileElementId:tmp.attr("id"),
            secureuri: false,
            dataType: 'json',                                                //服务器返回的格式，可以是json
            date:$('#updateForm').serialize(),
            // async:true,
            success: function (data,status){                                 //相当于java中try语句块的用法
                imgUrls += data.data+",";
                alert("success");
            },
            error: function (data, status, e){                               //相当于java中catch语句块的用法
                alert("fail");

            }
        });
        $('#'+id+'').before(tmp);
        $('#'+id+'').remove();

        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function(e){

            imgdata.push( this.result);

            var img  = $("<li><img src='"+this.result+"'></li>");

            var del = $("<i></i>");

            img.append(del);

            img.hover(function(){
                $(this).find("i").show();
            },function(){
                $(this).find("i").hide();
            });

            del.click(function(){
                debugger;
                var index =  $(this).parent().index();

                imgdata.splice(index,index+1);

                $(this).parent().remove();
            });

            $("#imageList").append(img);
        }

    });
})